<?php
// show all performance history of the swimmer.
function showPerformanceHistory () {
	echo "<div class='featured-post'>
            <div class='post-title'>
                <h2>Performance History</h2>
			</div>
			<div class='post-entry'>";
			
	// Get the page number so we know which page to display.
	$pageNum = $_GET['pagenum'];
	if (!(isset($pageNum))) { 
		$pageNum = 1; 
	}
			
	$myID = $_SESSION['ls_userid'];
	// Get all records of the swimmer from swimPerformance table.
	// Also get the events name, type, length, and date from ls_events table.
	$query = "SELECT `swimPerformance`.`swimmerID` AS swimmerID,
				`swimPerformance`.`eventID` AS swim_eventID,
				`ls_events`.`eventID` AS event_eventID
				FROM `swimPerformance`, `ls_events`
				WHERE swimmerID = '$myID'
				AND `swimPerformance`.`eventID` = `ls_events`.`eventID`";
	$list = mysql_query($query) or die("Error getting performance history 1: " . mysql_error());			
	// Get the total number of comments
	$totalRows = mysql_num_rows($list);
	
	// Show comments only if there are comments for the current user.
	if($totalRows == 0) {
		echo "<p>You have no performance history.</p>";
	}
	else {
		// Number of results to show per page.
		$rowLimit = 10;
		
		// Number for last page.
		$last = ceil($totalRows / $rowLimit);
		
		// Make sure page number is in range.
		if($pageNum < 1) {
			$pageNum = 1;
		}
		else if ($pageNum > $last) {
			$pageNum = $last;
		}
		
		// Set the limit range for the sql query. Use $max to get the subset of
		// records to display for the chosen page based on pagination.
		if($pageNum > 0) {
			$max = 'LIMIT ' .($pageNum - 1) * $rowLimit . ',' . $rowLimit;
		}
		
		// Get all records of the swimmer from swimPerformance table.
		// Also get the events name, type, length, and date from ls_events table.
		$query = "SELECT `swimPerformance`.`swimmerID` AS swimmerID,
					`swimPerformance`.`eventID` AS swim_eventID,
					`swimPerformance`.`time` AS time,
					`swimPerformance`.`place` AS place,
					`ls_events`.`eventID` AS event_eventID,
					`ls_events`.`name` AS eventName,
					`ls_events`.`eventType` AS eventType,
					`ls_events`.`eventLength` AS length,
					`ls_events`.`eventDate` AS date
					FROM `swimPerformance`, `ls_events`
					WHERE swimmerID = '$myID'
					AND `swimPerformance`.`eventID` = `ls_events`.`eventID`
					ORDER BY date DESC $max";
		
		$list = mysql_query($query) or die("Could not get performance history 2: " . mysql_error());

		// Print out the list of swim performance records in an html table.
		echo "<table id='blueTable'><tr><th>Event Name</th><th>Event Type</th><th>Length</th><th>Date</th><th>Time</th><th>Place</th></tr>";
		$i = 0;
		while($rows = mysql_fetch_assoc($list)) {
			// Use the $style variable to alternate the css class.
			$style = (($i % 2) == 0)? 'even' : 'odd';
			echo "<tr class='$style'><td>" . $rows['eventName'] . "</td><td>" . $rows['eventType'] . "</td><td>" . $rows['length'] . 
				"</td><td>" . $rows['date'] . "</td><td>" . $rows['time'] . "</td><td>" . $rows['place'] . "</td></tr>";
			$i++;
		}
		echo "</table>";
		
		// Do pagination if there are more than 1 pages of results to show.
		if($last > 1) {
			paginate($pageNum, $last);
		}	
	}
	echo "</div></div>";
}
?>